﻿using System;
using System.Windows.Input;
using GurLoader.Util;

namespace GurLoader.View
{
    public class DelegateCommand : ICommand
    {
        public Action CommandAction { get; set; }
        public Func<bool> CanExecuteFunc { get; set; }

        private IRequeryable Requeryable { get; set; }

        public DelegateCommand() : this(new CommandManagerWrapper())
        {
            
        }

        public DelegateCommand(IRequeryable requeryable)
        {
            Requeryable = requeryable;
        }

        public void Execute(object parameter)
        {
            CommandAction();
        }

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc == null || CanExecuteFunc();
        }

        public event EventHandler CanExecuteChanged
        {
            add { Requeryable.RequerySuggested += value; }
            remove { Requeryable.RequerySuggested -= value; }
        }

    }
}